// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: gekim

#pragma once

#include <map>
#include <memory>
#include <regex>
#include <string>
#include <vector>

// internal
#include "coin2/exchange/base/feed/processor.h"
#include "coin2/exchange/base/market/enums.h"

namespace coin2::exchange::kucoin::feed_v1 {
namespace impl {
using namespace coin2::exchange::base::symbology;

struct FeedTopicStat {
  bool snapshot_received = false;
  bool update_received = false;
};

class KucoinFeedProcessor : public base::feed::FeedParsingProcessor {
 public:
  explicit KucoinFeedProcessor(const std::string& system_name,
                               const coin2::exchange::di::ConstantInjection& ci,
                               const feed::RawParserConfig& config)
    : FeedParsingProcessor(
          system_name,
          MarketExchangeApi{MarketType::Spot, ExchangeType::Kucoin, "v1"},
          ci, config,
          "cc/coin2/exchange/kucoin/feed_v1/schema.json") {
  }

  void OnTopicRecord(const base::executor::TopicRecordPacket& packet) override;

 private:
  std::unordered_map<std::string, FeedTopicStat> topic_stat_;
  // 20221220T00:00:00
  static constexpr int64_t SNAPSHOT_ADDED_TS = 1'671'494'400'000'000'000LL;
};

struct KucoinChannel {
  std::string type;

  static KucoinChannel Parse(const std::string& ch) {
    /*
    "book-25"
    "trade"
    */
    std::vector<std::string> splitted;
    splitted = Split(ch, "-");
    CHECK_GE_THROW(splitted.size(), 1) << "unknown channel: " << ch;
    return KucoinChannel{splitted[0]};
  }
};
}  // namespace impl
using impl::KucoinFeedProcessor;
using impl::KucoinChannel;
}  // namespace coin2::exchange::kucoin::feed_v1
